| Conditions | 1 |
| Paths | 1 |
| Total Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | const fs = require('fs') |
||
| 7 | describe('Output helper', () => { |
||
| 8 | const spyError = jest.spyOn(console, 'error').mockImplementation(() => {}) |
||
|
|
|||
| 9 | const spyLog = jest.spyOn(console, 'log').mockImplementation(() => {}) |
||
| 10 | |||
| 11 | const file = 'test/output.txt' |
||
| 12 | const path = join(__dirname, '../../', file) |
||
| 13 | |||
| 14 | it('output has empty data', () => { |
||
| 15 | const result = outputHelper('', null) |
||
| 16 | expect(result).toBe(null) |
||
| 17 | }) |
||
| 18 | |||
| 19 | it('output is console', () => { |
||
| 20 | outputHelper(null, 'data') |
||
| 21 | expect(spyLog).toHaveBeenCalled() |
||
| 22 | expect(spyLog.mock.calls[0][0]).toContain('data') |
||
| 23 | }) |
||
| 24 | |||
| 25 | it('output is invalid', () => { |
||
| 26 | outputHelper(1, 'data') |
||
| 27 | expect(spyError).toHaveBeenCalled() |
||
| 28 | expect(spyError.mock.calls[0][0]).toContain('Invalid Output') |
||
| 29 | }) |
||
| 30 | |||
| 31 | it('output to file', done => { |
||
| 32 | outputHelper(file, 'data') |
||
| 33 | setTimeout(() => { |
||
| 34 | try { |
||
| 35 | expect(fs.readFileSync(path, 'utf8')).toBe('data') |
||
| 36 | done() |
||
| 37 | } catch (err) { |
||
| 38 | done.fail(err) |
||
| 39 | } |
||
| 40 | }, TIMEOUT) |
||
| 41 | }) |
||
| 42 | |||
| 43 | it('output to not existed file', done => { |
||
| 44 | const dummy = 'dummy.txt' |
||
| 45 | outputHelper(dummy, 'data') |
||
| 46 | setTimeout(() => { |
||
| 47 | try { |
||
| 48 | expect(spyError).toHaveBeenCalled() |
||
| 49 | expect(spyError.mock.calls[1][0]).toContain('Cannot write file to') |
||
| 50 | done() |
||
| 51 | } catch (err) { |
||
| 52 | done.fail(err) |
||
| 53 | } |
||
| 54 | }, TIMEOUT) |
||
| 55 | }) |
||
| 56 | |||
| 57 | it('output to write stream', done => { |
||
| 58 | const writeable = fs.createWriteStream(path) |
||
| 59 | outputHelper(writeable, 'data_stream') |
||
| 60 | setTimeout(() => { |
||
| 61 | try { |
||
| 62 | expect(fs.readFileSync(path, 'utf8')).toBe('data_stream\r\n') |
||
| 63 | done() |
||
| 64 | } catch (err) { |
||
| 65 | done.fail(err) |
||
| 66 | } |
||
| 67 | }, TIMEOUT) |
||
| 68 | }) |
||
| 69 | }) |
||
| 70 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.